package com.xps.charmingshop.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.xps.charmingshop.R;
import com.xps.charmingshop.adapter.CollocationListAdapter;
import com.xps.charmingshop.bean.MenuBean;
import com.xps.charmingshop.date.net.UserNetWorks;
import com.xps.charmingshop.utils.SharedPreferencesUtils;
import com.xps.charmingshop.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by MI on 2018/1/24.
 */

public class MenuActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.elv_collocation)
    ExpandableListView elvCollocation;
    List<MenuBean.ReturnBodyBean> returnBodyBeen;
    List<MenuBean.ReturnBodyBean.DishInfoBean> dishInfoBeen;

    @Override
    protected int getLayout() {
        return R.layout.activity_menu;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("我的菜单");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        getMenu();
    }

    @OnClick({R.id.actionbar_iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMenu();

    }

    public void getMenu() {
        String user_token = SharedPreferencesUtils.getString("user_token", "String", null);
        UserNetWorks.getMenu(user_token, new Subscriber<MenuBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(MenuBean menuBean) {
                returnBodyBeen = new ArrayList<>();
                dishInfoBeen = new ArrayList<>();

                if (menuBean.getReturn_code().equals("SUCCESS")) {
                    MenuBean.ReturnBodyBean shopInfobean = new MenuBean.ReturnBodyBean();
                    String shopName = shopInfobean.getSort_name();
                    //分类列表
                    List<MenuBean.ReturnBodyBean> list = menuBean.getReturn_body();
                    for (int i = 0; i < list.size(); i++) {
                        MenuBean.ReturnBodyBean bean = new MenuBean.ReturnBodyBean();
                        bean.setSort_name(list.get(i).getSort_name());
                        //分类里的菜品
                        List<MenuBean.ReturnBodyBean.DishInfoBean> list1 = list.get(i).getDish_info();
                        String img = null;
                        for (int k = 0; k < list1.size(); k++) {
                            MenuBean.ReturnBodyBean.DishInfoBean bean1 = new MenuBean.ReturnBodyBean.DishInfoBean();
                            bean1.dish_name = list1.get(k).getDish_name();
                            dishInfoBeen.add(bean1);
                        }
                        returnBodyBeen.add(bean);
                        //添加每一种类型的菜品
                        bean.setDish_info(list1);
                        elvCollocation.setAdapter(new CollocationListAdapter(MenuActivity.this, elvCollocation, returnBodyBeen));
                        elvCollocation.expandGroup(0);//默认展开第一个
                    }
                }
            }
        });
    }

}
